import { createActions, handleActions } from 'redux-actions';
import { getServerTime, defaultScanInterface } from '@api/system';

import _ from 'lodash';

const initState = {
  systemTime: '',
  Interface: ''
};

export const actions = createActions({
  GET_SERVER_TIME: params => {
    return getServerTime(params).then(data => {
      const time = _.get(data.data, 'data.timestamp');
      if (time) {
        return Promise.resolve(time);
      }
    });
  },
  // 获取默认扫描网口
  // 根据工单：ZOOMEYEBE-4276 目前已注释其功能
  DEFAULT_SCAN_INTER_FACE: params => {
    return defaultScanInterface(params).then(res => {
      const { data } = res;
      const Interface = data.data;
      if (Interface) {
        return Promise.resolve(Interface);
      }
    });
  }
});

const reducer = handleActions(
  {
    GET_SERVER_TIME: (state, actions) => {
      const { payload } = actions;
      return {
        ...state,
        systemTime: payload
      };
    },
    DEFAULT_SCAN_INTER_FACE: (state, actions) => {
      const { payload } = actions;
      return {
        ...state,
        Interface: _.get(payload, 'defaultInterface')
      };
    }
  },
  initState
);

export default reducer;
